<?php
header ('Content-type: text/html; charset=utf-8');
$xname='index';
if(isset($_GET['view']))$xname=$_GET['view'];
$xmenu=true;
if(isset($_GET['nomenu']))$xmenu=false;

require_once('apm-conf.inc');
require_once('lib/pwiki.php.inc');
require_once('lib/dot.php.inc');

?>

<html>
<head>

<script type="text/javascript" src="style/common/rounded.js"></script>
<script type="text/javascript" src="style/common/main.js"></script>
<script type="text/javascript" src="style/common/tablefilter.js" ></script>
<link   type="text/css"        href="style/common/main.css" rel="stylesheet"></link>

<?php echo "<script type='text/javascript' src='style/$xname.js'></script>"; ?>

<script type="text/javascript">
<?php
foreach ($_GET as $key => $value){
	echo "global_parameters[\"$key\"]=\"$value\";\n";
}
?>
</script>

<link   type="text/css"         href="style/<?php echo $xname; ?>.css"  rel="stylesheet"></link>

<title>apm :: <?php echo $xname; ?></title>

</head>

<body id="body" onLoad="displayResult();">

<?php if ( $xmenu ):  ?>

<!-- menu and title -->

<table border="0" id="menuTable" cellpadding="0" cellspacing="0" >
<tr>

<td id="menuPageTitle" nowrap="true"> &nbsp;&nbsp; <?php echo $apm_company; ?> :: Application <?php echo basename($xname); ?> &nbsp;&nbsp; </td>

<td id="menuTableCell"><ul id="menuList">
<?php
$styles=array();
foreach ( scandir ($apm_server_path.'/style/') as $filename) {
	if ( is_file($apm_server_path.'/style/'.$filename) && preg_match ( '/^[^.]+[.]xsl$/',$filename )  )
	    $styles[]=basename($filename,'.xsl');
}
//make index as the first item 
array_unshift($styles,'index');
$styles=array_unique($styles);
//do output

foreach ( $styles as $style) {
	echo "<a class=\"menuItem".($style==$xname?'Selected':'').
		"\" href=\"apm.php?view=$style\"><span class=\"menuItem\">".
		ucfirst ($style)."</span></a>";
}

?>
</ul></td>

</tr>
</table>
<br/>
<?php endif; ?>


<?php

$dotxsl=$apm_server_path.'/style/'.$xname.'.dot.xsl';
$dotdot=$apm_graph_path.$xname.'.dot';

$xmlDoc = new DOMDocument();
$xmlDoc->load($apm_data_path);

$xslDoc = new DOMDocument();
$proc = new XSLTProcessor();


if( file_exists ( $dotxsl ) ) {
	if( !file_exists($dotdot) || @filemtime ( $apm_data_path )!=filemtime ( $dotdot ) ){
		//regenerate dot graph
		$xslDoc->load($dotxsl);
		$proc->importStylesheet($xslDoc);
		file_put_contents ( $dotdot, trim($proc->transformToXML($xmlDoc)));
		if(@filemtime ( $apm_data_path )>1)touch($dotdot, @filemtime ( $apm_data_path ));
	}
	//generate map and include it into html
	readfile( dot($xname,'cmapx'));
}

$xslDoc->load($apm_server_path.'/style/'.$xname.'.xsl');
$proc->importStylesheet($xslDoc);

//set parameters for xslt processor
foreach ($_GET as $key => $value){
	if( preg_match ( '/^xs[A-Z].*/', $key) ) {
		$proc->setParameter('', $key, $value);
	}
}

pwiki($proc->transformToXML($xmlDoc));

?>

</body>
</html>
